package com.gherasoft.model;

import android.widget.BaseAdapter;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * User: sckomoroh
 * Date: 3/12/13
 * Time: 8:15 PM
 */
public class CheckListModel
{
    private List<CheckListItem> listItems = new ArrayList<CheckListItem>();
    private BaseAdapter adapter;
    private String fileName;
    private String listName;
    private boolean isSaved;
    private Date lastSaveTime;
    private CheckListModelListener listener;

    public CheckListModel(String fileName, String listName, Date modifiedDate)
    {
        this.fileName = fileName;
        this.listName = listName;
        this.isSaved = true;
        this.lastSaveTime = modifiedDate;
    }

    public CheckListModel(BaseAdapter adapter)
    {
        this.adapter = adapter;
        generateFileName();
        this.isSaved = false;
    }

    public CheckListModel(BaseAdapter adapter, String fileName, String listName)
    {
        this.adapter = adapter;
        this.fileName = fileName;
        this.listName = listName;
        this.isSaved = true;
    }

    public void setListener(CheckListModelListener listener)
    {
        this.listener = listener;
    }

    public boolean isSaved()
    {
        return this.isSaved;
    }

    public void setSaved()
    {
        this.isSaved = true;
        this.lastSaveTime = new Date(System.currentTimeMillis());
    }

    public Date getLastSaveTime()
    {
        return this.lastSaveTime;
    }

    public void setAdapter(BaseAdapter adapter)
    {
        this.adapter = adapter;
    }

    public void setListName(String listName)
    {
        this.listName = listName;
        setListChanged();
    }

    public String getListName()
    {
        return this.listName;
    }

    public String getFileName()
    {
        return this.fileName;
    }

    public int addItem(CheckListItem item)
    {
        this.listItems.add(item);
        setListChanged();
        notifyAdapter();

        return this.listItems.indexOf(item);
    }

    public int addItem(String itemName)
    {
        CheckListItem item = new CheckListItem(itemName);
        this.listItems.add(item);
        setListChanged();
        notifyAdapter();

        return this.listItems.indexOf(item);
    }

    public void removeItem(int index)
    {
        this.listItems.remove(index);
        setListChanged();
        notifyAdapter();
    }

    public CheckListItem getItem(int index)
    {
        return this.listItems.get(index);
    }

    public String getItemTitle(int index)
    {
        return this.listItems.get(index).getTitle();
    }

    public boolean isItemChecked(int index)
    {
        return this.listItems.get(index).isChecked();
    }

    public boolean isItemFailed(int index)
    {
        return this.listItems.get(index).isFailed();
    }

    public int getCount()
    {
        return this.listItems.size();
    }

    public void setItem(int index, String item)
    {
        CheckListItem itemModel = this.listItems.get(index);
        itemModel.setTitle(item);
        setListChanged();
        notifyAdapter();
    }

    public void setItemChecked(int index, boolean checked)
    {
        CheckListItem itemModel = this.listItems.get(index);
        itemModel.setChecked(checked);
        notifyAdapter();
    }

    public void setItemFailed(int index, boolean failed)
    {
        CheckListItem itemModel = this.listItems.get(index);
        itemModel.setFailed(failed);
        notifyAdapter();
    }

    public void saveList(String dataDir)
    {
        String fullFileName = String.format(
                "%s/%s",
                dataDir,
                this.getFileName());

        String content = String.format(
                "<checkList name='%s' modified='%s'>",
                this.getListName(),
                this.getLastSaveTime().toLocaleString());

        int itemCount = this.getCount();
        for (int i=0; i<itemCount; i++)
        {
            CheckListItem item = this.getItem(i);
            content += String.format(
                    "<item name='%s' checked='%s' failed='%s'/>",
                    item.getTitle(),
                    item.isChecked(),
                    item.isFailed());
        }

        content += "</checkList>";

        try
        {
            BufferedWriter writer = new BufferedWriter(new FileWriter(fullFileName));
            writer.write(content);
            writer.close();

            setSaved();
            notifyListSaved();
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }

    private void setListChanged()
    {
        this.isSaved = false;
        notifyListChanged();
    }

    private void generateFileName()
    {
        Random rand = new Random();

        String fileName = "";
        for (int i=0; i<16; i++)
        {
            char ch = (char)(rand.nextInt(26) + 97);
            fileName += ch;
        }

        this.fileName = fileName + ".xml";
    }

    private void notifyAdapter()
    {
        if (this.adapter != null)
        {
            this.adapter.notifyDataSetChanged();
        }
    }

    private void notifyListChanged()
    {
        if (listener != null)
        {
            listener.onModelChanged();
        }
    }

    private void notifyListSaved()
    {
        if (listener != null)
        {
            listener.onModelSaved(this.lastSaveTime);
        }
    }
}
